package main

import (
	"backend/app/dao"
	"backend/config"
	"backend/router"
	"backend/utils/logger"
	"backend/utils/snowflake"
	"fmt"
	"os"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Println("need config file eg: shopserver config.yaml")
		return
	}
	if err := config.Init(os.Args[1]); err != nil {
		fmt.Printf("setting init failed, err: %v\n", err)
		return
	}

	//初始化日志模块
	if err := logger.Init(config.ServerConf.LogConfig, config.ServerConf.Mode); err != nil {
		fmt.Printf("init logger failed, err:%v\n", err)
		return
	}

	//初始化id生成模块
	if err := snowflake.Init("2021-01-01", 1); err != nil {
		fmt.Printf("init snowflake failed, err:%v\n", err)
		return
	}

	if err := dao.InitDao(config.ServerConf.MySQLConfig, config.ServerConf.RedisConfig); err != nil {
		fmt.Printf("init dao failed, err: %v\n", err)
		return
	}
	r := router.SetupRouter(config.ServerConf.Mode, config.ServerConf.SgOpen)
	err := r.Run(fmt.Sprintf(":%d", config.ServerConf.Port))
	if err != nil {
		fmt.Errorf("run server error %v \n", err)
		return
	}
}
